<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Home\Model;
use Org\Util\String;
use Think\Model;

/**
 * Description of ShopppingCartModel
 *
 * @author Dark
 */
class ShoppingCartModel extends Model {
    
    /*
     * 增加商品进入购物车
     */
    public function addToCart($data) {
        if(!empty($data)) {
            $this->create();
            return $this->add($data);
        } else {
            return FALSE;
        }
    }
    
    /*
     * 删除购物车信息
     */
    public function deleteByID($data) {
        $sqlIn='';
        foreach($data as $k=>$v){
            $sqlIn=$sqlIn.$v.',';
        }
        $sqlIn = substr($sqlIn,0,strlen($sqlIn)-1);
        $sql="DELETE FROM EachGo.`t_shopping_cart`
              WHERE ID IN(".$sqlIn.") ";
        return $this->execute($sql);
        var_dump($sqlIn);
    }
    
    /*
     * 获取某个用户的购物车信息
     */
    public function getCart($userID) {
        $sql="SELECT b.ID AS goods_id,b.`unitPrice`,b.goodsName,a.goodsNumber,a.isBingo,b.`unitPrice`*a.goodsNumber AS totalMoney,
            a.`ID` AS cartId,b.currentNumber
            FROM EachGo.`t_shopping_cart` a
            JOIN EachGo.`t_goods` b
            ON a.goods_ID = b.`ID`
            WHERE user_ID=".$userID."
            ORDER BY a.addDate";
        return $this->query($sql);
    }
    
    /*
     * 更新购物车信息
     */
    public function updateCart($ID,$data) {
        if(!empty($ID)) {
            $this->where('ID='.$ID)->save($data);
        } else{
            return FALSE;
        }
    }
}
